package com.hhhn.wk.main.tab1.adapter;

/**
 * Created by Administrator on 2018/5/30 0030.
 */

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.hhhn.wk.R;
import com.hhhn.wk.main.tab2.entity.Commodity;
import com.hhhn.wk.utils.Constants;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lixu on 2017/6/27.
 */

public class RecyclerViewAdapter extends RecyclerView.Adapter implements View.OnClickListener {

    private LayoutInflater inflater;
    private Context context;

    private ArrayList<Commodity> mHostArrayList=new ArrayList<>();



    //声明自定义的监听接口
    private OnRecyclerviewItemClickListener mOnRecyclerviewItemClickListener = null;

    //构造方法中添加自定义监听接口
    public RecyclerViewAdapter(Context context,ArrayList<Commodity> mHostArrayList,OnRecyclerviewItemClickListener mOnRecyclerviewItemClickListener) {
        this.context = context;
        this.mHostArrayList = mHostArrayList;
        inflater = LayoutInflater.from(context);
        this.mOnRecyclerviewItemClickListener = mOnRecyclerviewItemClickListener;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.layout_image_item, parent, false);
        //这里 我们可以拿到点击的item的view 对象，所以在这里给view设置点击监听，
        view.setOnClickListener(this);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder holder1 = (MyViewHolder) holder;
        Commodity xiaoshuoBean = mHostArrayList.get(position);

        holder1.title.setText(xiaoshuoBean.getCommodityName());
        holder1.priceNew.setText("￥"+xiaoshuoBean.getPriceNew());
        holder1.priceOld.setText("￥"+xiaoshuoBean.getPriceOld());
        holder1.inventoryNumber.setText("剩余："+xiaoshuoBean.getInventoryNumber());
        Glide.with(context).load(Constants.HTTPIMG+xiaoshuoBean.getCommodityImg()).into( holder1.img);
        holder1.itemView.setTag(position);//给view设置tag以作为参数传递到监听回调方法中
    }

    @Override
    public int getItemCount() {
        return mHostArrayList.size();
    }

    @Override
    public void onClick(View v) {
        //将监听传递给自定义接口
        mOnRecyclerviewItemClickListener.onItemClickListener(v, ((int) v.getTag()));
    }


    class MyViewHolder extends RecyclerView.ViewHolder{
        private TextView title;
        private TextView priceNew;
        private TextView priceOld;
        private TextView inventoryNumber;
        private ImageView img;
        public MyViewHolder(View itemView) {
            super(itemView);

            title =  ((TextView) itemView.findViewById(R.id.iv_CommodityName));
            priceNew = ((TextView) itemView.findViewById(R.id.tv_priceNew));
            priceOld = ((TextView) itemView.findViewById(R.id.tv_priceOld));
            inventoryNumber = ((TextView) itemView.findViewById(R.id.tv_inventoryNumber));
            img = ((ImageView) itemView.findViewById(R.id.iv_CommodityImg));

        }
    }

}